package servlets;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import java.io.IOException;
import db.DriversDao;
import service.EmployeeService;
import beans.Drivers;
import beans.Employee;

@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
    private DriversDao driversDao = new DriversDao();
    private EmployeeService employeeService = new EmployeeService();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        
        // 获取登录信息
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String role = request.getParameter("role");
        
        System.out.println("=== 登录请求开始 ===");
        System.out.println("用户名: " + username);
        System.out.println("角色: " + role);
        System.out.println("密码长度: " + (password != null ? password.length() : 0));

        boolean isValid = false;
        Employee employee = null;
        Drivers driver = null;
        
        // 根据角色进行不同的验证
        switch (role) {
            case "driver":
                System.out.println("尝试司机登录验证");
                driver = driversDao.validateDriverLogin(username, password);
                if (driver != null) {
                    System.out.println("司机登录验证成功");
                    isValid = true;
                } else {
                    System.out.println("司机登录验证失败");
                }
                break;
            case "driverLeader":
                System.out.println("尝试司机队长登录验证");
                employee = employeeService.validateDriverLeaderLogin(username, password);
                if (employee != null) {
                    System.out.println("司机队长登录验证成功");
                    isValid = true;
                } else {
                    System.out.println("司机队长登录验证失败");
                }
                break;
            case "employee":
                System.out.println("尝试普通员工登录验证");
                employee = employeeService.validateNormalEmployeeLogin(username, password);
                if (employee != null) {
                    System.out.println("普通员工登录验证成功");
                    isValid = true;
                } else {
                    System.out.println("普通员工登录验证失败");
                }
                break;
            case "finance":
                System.out.println("尝试财务登录验证");
                employee = employeeService.validateFinanceLogin(username, password);
                if (employee != null) {
                    System.out.println("财务登录验证成功");
                    isValid = true;
                } else {
                    System.out.println("财务登录验证失败");
                }
                break;
            default:
                System.out.println("未知角色: " + role);
        }
        
        if (isValid) {
            System.out.println("登录验证通过，准备创建session");
            // 创建新的session
            HttpSession oldSession = request.getSession(false);
            if (oldSession != null) {
                System.out.println("清除旧session");
                oldSession.invalidate();
            }
            HttpSession session = request.getSession(true);
            
            // 存储用户信息到session
            session.setAttribute("username", username);
            session.setAttribute("role", role);
            
            // 根据角色存储不同的对象
            if (role.equals("driver")) {
                session.setAttribute("user", driver);
                session.setAttribute("driverId", driver.getDriverId());
                session.setAttribute("driverName", driver.getDriverName());
                System.out.println("存储司机信息到session");
            } else {
                session.setAttribute("user", employee);
                System.out.println("存储员工信息到session");
            }
            
            // 设置session超时时间（30分钟）
            session.setMaxInactiveInterval(30 * 60);
            
            System.out.println("准备重定向到对应页面");
            String contextPath = request.getContextPath();
            System.out.println("上下文路径: " + contextPath);
            
            // 根据角色重定向到不同页面
            switch (role) {
                case "driver":
                    System.out.println("重定向到司机页面");
                    response.sendRedirect(contextPath + "/DriversServlet?action=1234");
                    break;
                case "driverLeader":
                    System.out.println("重定向到司机队长页面");
                    response.sendRedirect(contextPath + "/DriversServlet?action=all3");
                    break;
                case "employee":
                    System.out.println("重定向到普通员工页面");
                    response.sendRedirect(contextPath + "/DriversServlet?action=123");
                    break;
                case "finance":
                    System.out.println("重定向到财务页面");
                    response.sendRedirect(contextPath + "/expenseClaim.jsp");
                    break;
                default:
                    System.out.println("未知角色，重定向到登录页面");
                    response.sendRedirect(contextPath + "/login.jsp");
            }
        } else {
            System.out.println("登录验证失败，返回登录页面");
            // 登录失败，重定向回登录页面
            request.setAttribute("error", "用户名或密码错误，或您没有该角色的权限");
            request.getRequestDispatcher("login.jsp").forward(request, response);
        }
        System.out.println("=== 登录请求结束 ===");
    }
} 